package org.ceteca.explica.client.partners;



import java.util.HashSet;

import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.InterfaceErrors;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.PartnerOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.util.Validator;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;


/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class ContactClient extends CommonClient {

	
	// WIDGETS
	Text entryContactCode, entryContactName, entryContactPosition,
			entryContactFax, entryContactEmail, entryContactPhone, entryContactPartner, entryVersion;
	Button buttonSelectPartner;
	
	
	public ContactClient() {
		super(ContactOT.class, "","");
		setEditGladePath(InterfacePartners.EXPLICA_EDIT_CONTACT_GLADE_PATH);
		setDialogName(InterfacePartners.EDIT_CONTACT_DIALOG_NAME);
		
	}



	@Override
	public void clearEntityData() {

		ContactOT c = new ContactOT();
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity()
					.getEntity().getKey().equals(
							InterfacePartners.EXPLICA_ENTITY_PARTNER_ID)) {
				PartnerOT parent = (PartnerOT) entityDetailView
						.getSubentityData().getParentEntity()
						.getSelectedEntity();
				c.setPartner(parent);
				entryContactPartner.setEnabled(false);
				buttonSelectPartner.setEnabled(false);

			}
		} else {
			entryContactPartner.setText("");
			entryContactPartner.setEnabled(false);
			buttonSelectPartner.setEnabled(false);
		}
		setWidgetsValues(c);

		
		

		

	}





	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((ContactOT) selectedEntity);
		}
	}

	@Override
	public BaseVO getEntityData() {

		ContactOT entity = new ContactOT();
		if (this.entEditView != null) {
			getWidgets();
			entity.setName(entryContactName.getText());
			entity.setPhone(entryContactPhone.getText());
			entity.setPosition(entryContactPosition.getText());
			entity.setFax(entryContactFax.getText());
			entity.setEmail(entryContactEmail.getText());
			entity.setPartner((PartnerOT)entryContactPartner.getData());
			
			if (selectedEntity != null) {
				entity.setVersion(((ContactOT) this.selectedEntity)
								.getVersion());
				entity.setIdContact(((ContactOT) this.selectedEntity)
						.getIdContact());
			}
		}

		return entity;
	}

	public void getWidgets() {
		entryContactCode = (Text) this.getWidget(
				InterfacePartners.EDIT_CONTACT_CODE_WIDGETNAME);
		entryContactName = (Text) this.getWidget(
				InterfacePartners.EDIT_CONTACT_NAME_WIDGETNAME);
		entryContactPosition = (Text) this.getWidget(
				InterfacePartners.EDIT_CONTACT_POSITION_WIDGETNAME);
		entryContactEmail = (Text) this.getWidget(
				InterfacePartners.EDIT_CONTACT_EMAIL_WIDGETNAME);
		entryContactPhone = (Text) this.getWidget(
				InterfacePartners.EDIT_CONTACT_PHONE_WIDGETNAME);
		entryContactFax = (Text) this.getWidget(
				InterfacePartners.EDIT_CONTACT_FAX_WIDGETNAME);
		entryContactPartner = (Text) this.getWidget(
				InterfacePartners.EDIT_CONTACT_PARTNER_WIDGETNAME);
		if ((entryVersion = ((Text) this
				.getWidget(
						InterfacePartners.EDIT_VERSION_WIDGETNAME))) == null) {
			fireExceptionAndExit("Error getting entry version wigdet");

		}
		buttonSelectPartner = ((Button) this
				.getWidget(
						InterfacePartners.EDIT_CONTACT_PARTNER_SELECT_WIDGETNAME));

		
		
		if (entryContactCode==null)
			this.logger.error("Error getting entry Contact Code Widget");
		if (entryContactName==null)
			this.logger.error("Error getting entry Contact Name Widget");
		if (entryContactPhone==null)
			this.logger.error("Error getting entry Contact Phone Widget");
		if (entryContactEmail==null)
			this.logger.error("Error getting entry Contact email Widget");
		if (entryContactFax==null)
			this.logger.error("Error getting entry Contact Fax Widget");
		if (entryContactPosition==null)
			this.logger.error("Error getting entry Contact Position Widget");
		if (entryContactPartner==null)
			this.logger.error("Error getting entry Contact Partner Widget");
		if (buttonSelectPartner==null)
			fireExceptionAndExit("Error getting entry Contact Select Partner Widget");
	}

	
	public void setWidgetsValues(BaseVO entity) {

		
		getWidgets();
		ContactOT ent=(ContactOT)entity;
		entryContactCode.setText(Converter.getInstance().toString(ent.getIdContact()));
		entryContactName.setText(ent.getName()==null?"":ent.getName());
		entryContactPosition.setText(ent.getPosition()==null?"":ent.getPosition());
		entryContactPhone.setText(ent.getPhone()==null?"":ent.getPhone());
		entryContactFax.setText(ent.getFax()==null?"":ent.getFax());
		entryContactEmail.setText(ent.getEmail()==null?"":ent.getEmail());
		entryVersion.setText(Converter.getInstance().toString(ent.getVersion()));
		if (ent.getPartner()!=null){
			entryContactPartner.setText(ent.getPartner().getName()==null?"":ent.getPartner().getName());
			entryContactPartner.setData(ent.getPartner());
		}
	}





	@Override
	public OperationResult validateEntity() {
		OperationResult result=new OperationResult();
		boolean datosValidos=true;
		ContactOT entity= (ContactOT)getEntityData();


			
		
		if (!entity.getEmail().isEmpty() && !Validator.getInstance().isValidEmail((entity.getEmail()))){
				
			result.addResultMessage(OperationResult.RESULT_OK, 
        			ClientContext.getInstance().getMsgCatalog().newResultMessage(InterfaceErrors.MSG_EMAIL_VALIDATION_ERROR));
				
			datosValidos=false;
		}
		if (!entity.getPhone().isEmpty() && !Validator.getInstance().isValidPhone((entity.getPhone()))){
			
			result.addResultMessage(OperationResult.RESULT_OK, 
        			ClientContext.getInstance().getMsgCatalog().newResultMessage(InterfaceErrors.MSG_PHONE_VALIDATION_ERROR));
				
			datosValidos=false;
		}

		
		if (!entity.getFax().isEmpty() && !Validator.getInstance().isValidPhone((entity.getFax()))){
			
			result.addResultMessage(OperationResult.RESULT_OK, 
        			ClientContext.getInstance().getMsgCatalog().newResultMessage(InterfaceErrors.MSG_PHONE_VALIDATION_ERROR));
				
			datosValidos=false;
		}
		
		
		if ( datosValidos)
			result.addResultMessage(OperationResult.RESULT_OK, 
        			ClientContext.getInstance().getMsgCatalog().newResultMessage(InterfaceErrors.MSG_VALIDATE_OK));
		else
			result.addResultMessage(OperationResult.RESULT_ERROR, 
        			ClientContext.getInstance().getMsgCatalog().newResultMessage(InterfaceErrors.MSG_VALIDATE_ERROR));
		return result;
	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryContactCode.setEnabled(false);
		entryContactEmail.setEnabled(enabled);
		entryContactFax.setEnabled(enabled);
		entryContactName.setEnabled(enabled);
		entryContactPartner.setEnabled(enabled);
		entryContactPhone.setEnabled(enabled);
		entryContactPosition.setEnabled(enabled);
		entryVersion.setEnabled(false);
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity()
					.getEntity().getKey().equals(
							InterfacePartners.EXPLICA_ENTITY_PARTNER_ID)) {
				entryContactPartner.setEnabled(false);
				buttonSelectPartner.setEnabled(false);
			}
		}
		
	}



	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_PARTNER_ID,
				InterfacePartners.EDIT_CONTACT_PARTNER_WIDGETNAME,
				this, buttonSelectPartner));

		lMultiSelectionSubentities = new HashSet<MultiSelectionSubentityOT>();
	}

	
	
	
	
	
	
	
	
	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}




	
	
	
	
	
}
